/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.chenweihuacwh.web.baseData;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.opensymphony.xwork2.ActionSupport;
import com.javaeye.chenweihuacwh.web.PageInfo;
import com.javaeye.chenweihuacwh.dto.baseData.Product;
import com.javaeye.chenweihuacwh.service.baseData.IProductCategoryService;
import com.javaeye.chenweihuacwh.service.baseData.IProductService;
import com.javaeye.chenweihuacwh.dto.baseData.ProductCategory;

public class ProductAction extends ActionSupport {
	
	private static Log log = LogFactory.getLog(ProductAction.class); 
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -3216585574200112708L;

	/**
	 * 用于显示一览结果集
	 */
	private List<Product> productList;
	
	/**
	 * 用于接收分页值对象
	 */
	private PageInfo pageInfo;
	
	/**
	 * 用于接收查询条件值对象
	 */
	private Product condition;
	
	/**
	 * 用于接收添加画面和编辑画面的值对象
	 */
	private Product product;
	
	/**
	 * 用于接收需要修改和删除的ID值
	 */
	private String productId;
	
	/**
	 * 用于生成查询画面上选择分类的下拉框的值
	 */
	private List<ProductCategory> categoryList;
	
	/**
	 * 用于编辑画面上选择分类的下拉框的值
	 */
	private List<ProductCategory> productNameList;
	
	
	/**
	 * 应用autocomplete的查询使用接口
	 */
	private String q;
	
	/**
	 * Service
	 */
	private IProductService productService;
	
	private IProductCategoryService categoryService;
	
	public ProductAction() {
		pageInfo = new PageInfo();
		pageInfo.setPageIndex(0);
		condition = new Product();
		condition.setName("");
		condition.setCategory("-1");
	}
	
	public String queryProductNames() throws Exception {
		productList = productService.getAutoCompleteProducts(q, 10);
		for (Product product : productList) {
			product.setCategory(categoryService.getCategory(product.getCategory()).getName());
		}
		return SUCCESS;
	}
	
	/**
	 * 条件分页查询
	 * @return
	 * @throws Exception
	 */
	public String queryProductList() throws Exception {
		log.debug("currentPage=" + pageInfo.getPageIndex() + 
				", category=" + condition.getCategory() + 
				", name=" + condition.getName());
		categoryList = categoryService.getCategorys("0");
		productList = productService.getProductList(condition, pageInfo);
		Product product;
		ProductCategory category;
		for (Object o : productList) {
			product = (Product)o;
			category = categoryService.getCategory(product.getCategory());
			if (category != null) {
				product.setCategory(category.getName());
			}
		}
		return SUCCESS;
	}
	
	/**
	 * 保存产品
	 * @return
	 * @throws Exception
	 */
	public String saveProduct() throws Exception {
		boolean success = productService.saveProduct(product);
		if (!success) {
			List<Object> params = new ArrayList<Object>();
			params.add("产品");
			this.addActionError(getText("actionErrorSaveUNIQUE", params));
			categoryList = categoryService.getCategorys("0");
			productNameList = categoryService.getCategorys(product.getCategory());
			return INPUT;
		}
		return SUCCESS;
	}
	
	/**
	 * 查询产品
	 * @return
	 * @throws Exception
	 */
	public String querySingleProduct() throws Exception {
		categoryList = categoryService.getCategorys("0");
		product = productService.getProduct(productId);
		productNameList = categoryService.getCategorys(product.getCategory());
		return SUCCESS;
	}
	
	/**
	 * 查询产品分类用于添加产品
	 * @return
	 * @throws Exception
	 */
	public String queryToAddProduct() throws Exception {
		categoryList = categoryService.getCategorys("0");
		productNameList = new ArrayList<ProductCategory>();
		return SUCCESS;
	}
	
	/**
	 * 删除产品
	 * @return
	 * @throws Exception
	 */
	public String deleteProduct() throws Exception {
		boolean result = productService.removeProduct(productId);
		if (!result) {
			List<Object> params = new ArrayList<Object>();
			params.add("产品");
			this.addActionError(getText("actionErrorDeleteFK", params));
			queryProductList();
			return INPUT;
		}
		return SUCCESS;
	}
	
	public List<Product> getProductList() {
		return productList;
	}

	public void setProductList(List<Product> productList) {
		this.productList = productList;
	}

	public PageInfo getPageInfo() {
		return pageInfo;
	}

	public void setPageInfo(PageInfo pageInfo) {
		this.pageInfo = pageInfo;
	}

	public Product getCondition() {
		return condition;
	}

	public void setCondition(Product condition) {
		this.condition = condition;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public String getProductId() {
		return productId;
	}

	public void setProductId(String productId) {
		this.productId = productId;
	}

	public List<ProductCategory> getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(List<ProductCategory> categoryList) {
		this.categoryList = categoryList;
	}

	public void setProductService(IProductService productService) {
		this.productService = productService;
	}

	public void setCategoryService(IProductCategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public String getQ() {
		return q;
	}

	public void setQ(String q) {
		this.q = q;
	}

	public List<ProductCategory> getProductNameList() {
		return productNameList;
	}

	public void setProductNameList(List<ProductCategory> productNameList) {
		this.productNameList = productNameList;
	}

}
